/*
 * (swing1.1beta3) jfc#96
 */
package jp.gr.java_conf.tame.swing.table;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.TableColumn;

import main.SportShop;

import org.salespointframework.core.data.CatalogItemImpl;
import org.salespointframework.core.data.interfaces.Catalog;


// TODO: Auto-generated Javadoc
/**
 * The Class MultiSpanCellTableExample.
 *
 * @version 1.0 11/26/98
 */
public class MultiSpanCellTableExample extends JFrame {

  /** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

/**
 * Instantiates a new multi span cell table example.
 */
MultiSpanCellTableExample() {
    super( "Multi-Span Cell Example" );
    
    AttributiveCellTableModel ml = new AttributiveCellTableModel(70,9);
    ml.setValueAt("Zeiten", 0, 0);
    ml.setValueAt("Montag", 0, 2);
    ml.setValueAt("Dienstag", 0, 3);
    ml.setValueAt("Mittwoch", 0, 4);
    ml.setValueAt("Donnerstag", 0, 5);
    ml.setValueAt("Freitag", 0, 6);
    ml.setValueAt("Samstag", 0, 7);
    ml.setValueAt("Sonntag", 0, 8);
    final CellSpan cellAtt =(CellSpan) ml.getCellAttribute();
    final ColoredCell cellAtt1 =(ColoredCell) ml.getCellAttribute();
    final MultiSpanCellTable table = new MultiSpanCellTable( ml );
    table.setDefaultRenderer(Object.class ,new AttributiveCellRenderer());
    int[] columns1 = {0,1 };
	int[] rows1    = {0};
    cellAtt.combine(rows1,columns1);
    for (int i = 6;i<=21;i++){
    	int[] columns = {0 };
    	int[] rows    = {((i-6)*4)+1,(i-6)*4+2,(i-6)*4+3,(i-6)*4+4  };

    	ml.setValueAt(i+"", ((i-6)*4)+1, 0);
    	ml.setValueAt("00-15", ((i-6)*4)+1, 1);
    	ml.setValueAt("15-30", ((i-6)*4)+2, 1);
    	ml.setValueAt("30-45", ((i-6)*4)+3, 1);
    	ml.setValueAt("45-60", ((i-6)*4)+4, 1);
    	cellAtt.combine(rows,columns);
    	cellAtt1.setBackground(new Color(0xFF6600), rows, columns);
    	cellAtt.combine(rows,columns);
    }
    /*
    AttributiveCellTableModel ml = new AttributiveCellTableModel(10,6) {
      public Object getValueAt(int row, int col) { 
        return "" + row + ","+ col; 
      }
    };
    */

    JScrollPane scroll = new JScrollPane( table );
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    TableColumn col = table.getColumnModel().getColumn(0);
 // daruaf kannst du dann unter anderen diese Methoden anwenden:
 col.setPreferredWidth(30);
col=table.getColumnModel().getColumn(1);
col.setPreferredWidth(40);

	int[] columns = {2 };
	int[] rows    = {1,2 };
	 ml.setValueAt("<HTML>Fussball<br>Halle 3</HTML>", 1, 2);
	cellAtt.combine(rows,columns);
	
	Catalog<CatalogItemImpl> events=SportShop.getEventCatalog();
	for (CatalogItemImpl event:events) {
		data.Event bla=(data.Event)event;
		System.out.println(bla.getEventName() + " "+bla.getTime());
		
	}
    JButton b_one   = new JButton("Combine");
    b_one.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	int[] columns = table.getSelectedColumns();
	int[] rows    = table.getSelectedRows();
	cellAtt.combine(rows,columns);
	table.clearSelection();
	table.revalidate();
	table.repaint();
      }
    });
    JButton b_split = new JButton("Split");
    b_split.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	int column = table.getSelectedColumn();
	int row    = table.getSelectedRow();
	cellAtt.split(row,column);
	table.clearSelection();
	table.revalidate();
	table.repaint();
      }
    });
    JPanel p_buttons = new JPanel();
    p_buttons.setLayout(new GridLayout(2,1));
    p_buttons.add(b_one);
    p_buttons.add(b_split);

    Box box = new Box(BoxLayout.X_AXIS);
    box.add(scroll);
    box.add(new JSeparator(SwingConstants.HORIZONTAL));
    box.add(p_buttons);
    getContentPane().add( box );
    setSize( 400, 200 );
    setVisible(true);
  }

  /**
   * The main method.
   *
   * @param args the arguments
   */
  public static void main(String[] args) {
    MultiSpanCellTableExample frame = new MultiSpanCellTableExample();
    frame.addWindowListener( new WindowAdapter() {
      @Override
	public void windowClosing( WindowEvent e ) {
        System.exit(0);
      }
    });
  }
}

